<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.prime.com.tr/ui">
    <ui:composition template="/view/template.xhtml">
        <ui:define name="head">
            <script type="text/javascript">
                    function handleSaveRequest(xhr, status, args) {
                            if(args.success) {
                                    commodityDialog.hide();
                            } else if(args.duplicate ){
                                     commodityDialog.hide();
                            }
                    }
            </script>
        </ui:define>
        <ui:define  name="content">
            <f:view>
                <h:form id ="form1">
                    <p:dataTable value="#{commodityManager.commodityList}"  var="item" id="commodities" paginator="true" rows="20" paginatorPosition="bottom">
                        <f:facet name="header">
                            <h:panelGrid columns="2" cellpadding="0" cellspacing="0">
                                <p:breadCrumb preview="true" expandEffectDuration="200" previewWidth="20" style="width:400px" expandedBeginningItems="1" expandedEndItems="0">
                                    <p:menuitem value="选项"/>
                                    <p:menuitem value="新增材料" actionListener="#{commodityManager.createMaterial}" oncomplete="commodityDialog.show();" update="form2:display"/>
                                    <p:menuitem value="新增实物商品" actionListener="#{commodityManager.createRetail}" oncomplete="commodityDialog.show();" update="form2:display"/>
                                    <p:menuitem value="新增虚拟商品" actionListener="#{commodityManager.createVirtual}" oncomplete="commodityDialog.show();" update="form2:display"/>
                                    <p:menuitem value="新增加工商品" actionListener="#{commodityManager.createWorking}" oncomplete="commodityDialog.show();" update="form2:display"/>
                                    <p:menuitem value="新增套系商品" actionListener="#{commodityManager.createPackage}" oncomplete="commodityDialog.show();" update="form2:display"/>
                                    <p:menuitem value="编辑套系商品" actionListener="#{commodityManager.editPackage}" oncomplete="packageCommodityDialog.show();" update="form3:packageDisplay"/>
                                </p:breadCrumb>
                                <h:panelGrid columns="6" style="width: 500px">
                                    <h:outputLabel value="模糊查询"></h:outputLabel>
                                    <h:inputText value="#{commodityManager.searchContent}" title="可输入部分商品与名称进行查询"></h:inputText>
                                    <p:commandLink update="form1:commodities" actionListener="#{commodityManager.seachByContent}">
                                        <p:graphicImage value="/resources/images/search.png"/>
                                    </p:commandLink>
                                    <p:ajaxStatus  style="width:16px;height:16px;">
                                        <f:facet name="start">
                                            <h:graphicImage  value="/resources/images/ajaxloading.gif" />
                                        </f:facet>
                                        <f:facet name="complete">
                                            <h:outputText value=""/>
                                        </f:facet>
                                    </p:ajaxStatus>
                                    <h:selectOneMenu value="#{commodityManager.commodityProperty}">
                                        <f:selectItem itemLabel="商品属性"></f:selectItem>
                                        <f:selectItems value="#{applicationBean.commdoityPropertyItems}"></f:selectItems>
                                        <p:ajax  update="form1:commodities,form1:commodityTypes" listener="#{commodityManager.searchByProperty}" />
                                    </h:selectOneMenu>
                                    <h:selectOneMenu id="commodityTypes" value="#{commodityManager.commodityType}">
                                        <f:selectItem itemLabel="商品类型"></f:selectItem>
                                        <f:selectItems value="#{commodityManager.commodityTypeItems}"></f:selectItems>
                                        <p:ajax  update="form1:commodities" listener="#{commodityManager.searchByType}" />
                                    </h:selectOneMenu>
                                </h:panelGrid>
                            </h:panelGrid>
                        </f:facet>
                        <p:column expansion="#{item.superCommdity}" />
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="属性"/>
                            </f:facet>
                            <h:outputText value="#{item.property}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="名称"/>
                            </f:facet>
                            <h:outputText value="#{item.name}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="编码"/>
                            </f:facet>
                            <h:outputText value="#{item.number}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="库存"/>
                            </f:facet>
                            <h:outputText value="#{item.stocks}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="单位"/>
                            </f:facet>
                            <h:outputText value="#{item.unit}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="类型"/>
                            </f:facet>
                            <h:outputText value="#{item.type}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="单价"/>
                            </f:facet>
                            <h:outputText value="#{item.uitPrice}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="成本"/>
                            </f:facet>
                            <h:outputText value="#{item.unitCost}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="启用"/>
                            </f:facet>
                            <h:selectBooleanCheckbox disabled="true" value="#{item.active}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="可否打折"/>
                            </f:facet>
                            <h:selectBooleanCheckbox disabled="true" value="#{item.discountable}"/>
                        </p:column>

                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="规格"/>
                            </f:facet>
                            <h:outputText value="#{item.standard}"/>
                        </p:column>
                        <p:column>
                            <f:facet name="header">
                                <h:outputText value="编辑"/>
                            </f:facet>
                            <p:commandLink action="#{commodityManager.editCommodity(item)}"  update="form2:display" oncomplete="commodityDialog.show()">
                                <p:graphicImage value="/resources/images/edit.png"/>
                            </p:commandLink>
                        </p:column>

                        <f:facet name="expansion">
                            <p:dataTable id="subItems" var="subItem" value="#{item.subCommodityCollection}" >
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="属性"/>
                                    </f:facet>
                                    <h:outputText value="#{subItem.subcommodity.property}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="名称"/>
                                    </f:facet>
                                    <h:outputText value="#{subItem.subcommodity.name}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="编码"/>
                                    </f:facet>
                                    <h:outputText value="#{subItem.subcommodity.number}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="数量"/>
                                    </f:facet>
                                    <h:outputText value="#{subItem.amount}"/>
                                </p:column>
                            </p:dataTable>

                        </f:facet>
                    </p:dataTable>
                </h:form>

                <p:dialog header="编辑商品" widgetVar="commodityDialog"
                          modal="true" draggable="false" width="700" closeListener="#{commodityManager.cancellCreate}"  onCloseUpdate="form:grow1">
                    <h:form id="form2">
                        <h:panelGrid columns="2" id="display" columnClasses="columnTop,columnTop">
                            <h:panelGrid columns="2" >
                                <h:outputLabel value="属性:" for="property" />
                                <h:inputText id="property" value="#{commodityManager.selectedCommodity.property}" title="Property" disabled="true"/>
                                <h:outputLabel value="名称:" for="name" />
                                <h:inputText id="name" value="#{commodityManager.selectedCommodity.name}" title="Name" validator="#{commodityManager.checkUniqueCommoditName}">
                                    <p:ajax event="blur" update="form:growl"/>
                                </h:inputText>
                                <h:outputLabel value="编码:" for="number" />
                                <h:inputText id="number" value="#{commodityManager.selectedCommodity.number}" title="Number"  validator="#{commodityManager.checkUniqueCommoditNumber}">
                                    <p:ajax event="blur" update="form:growl"/>
                                </h:inputText>
                                <h:outputLabel value="库存:" for="stocks" rendered="#{commodityManager.selectedCommodity.stock}"/>
                                <h:inputText id="stocks" value="#{commodityManager.selectedCommodity.stocks}"  rendered="#{commodityManager.selectedCommodity.stock}" title="Stocks" validatorMessage="库存数量输入错误">
                                    <f:validateLongRange minimum="0" for="stocks" ></f:validateLongRange>
                                </h:inputText>
                                <h:outputLabel value="单位:" for="unit" />
                                <h:selectOneMenu id="unit" value="#{commodityManager.selectedCommodity.unit}" required="true" requiredMessage="请选择商品单位">
                                    <f:selectItem itemLabel="商品单位"></f:selectItem>
                                    <f:selectItems value="#{applicationBean.unitItems}"></f:selectItems>
                                </h:selectOneMenu>
                                <h:outputLabel value="类型:" for="commodityTypesForEdit" />
                                <h:selectOneMenu id="commodityTypesForEdit" value="#{commodityManager.selectedCommodity.type}" required="true" requiredMessage="请选择商品类型">
                                    <f:selectItem itemLabel="商品类型"></f:selectItem>
                                    <f:selectItems value="#{commodityManager.commodityTypeItemsForEdit}"></f:selectItems>
                                </h:selectOneMenu>

                                <h:outputLabel value="单价:" for="uitPrice" />
                                <h:inputText id="uitPrice" value="#{commodityManager.selectedCommodity.uitPrice}" title="UitPrice"  validatorMessage="单价输入错误">
                                    <f:validateDoubleRange minimum="0" for="uitPrice"></f:validateDoubleRange>
                                </h:inputText>
                                <h:outputLabel value="成本:" for="unitCost"  rendered="#{commodityManager.selectedCommodity.stock}"/>
                                <h:inputText id="unitCost" value="#{commodityManager.selectedCommodity.unitCost}" title="UnitCost"  rendered="#{commodityManager.selectedCommodity.stock}"/>
                                <h:outputLabel value="可打折:" for="discountable" />
                                <h:selectBooleanCheckbox id="discountable" value="#{commodityManager.selectedCommodity.discountable}" title="Discountable" />
                                <h:outputLabel value="启用:" for="active" />
                                <h:selectBooleanCheckbox id="active" value="#{commodityManager.selectedCommodity.active}" title="Active" />
                                <h:outputLabel value="规格:" for="standard"  rendered="#{commodityManager.selectedCommodity.stock}"/>
                                <h:inputText id="standard" value="#{commodityManager.selectedCommodity.standard}" title="Standard"  rendered="#{commodityManager.selectedCommodity.stock}" />
                            </h:panelGrid>
                            <p:dataTable id="commodityRoyalties" value="#{commodityManager.selectedCommodity.commodityRoyaltyCollection}" var="item" rowIndexVar="rowIndex">
                                <f:facet name="header">
                                    <h:selectOneMenu value="#{commodityManager.selectedDepartmentPost}">
                                        <f:selectItems value="#{applicationBean.departmentPostSelectItems}"/>
                                        <p:ajax  update="form2:commodityRoyalties,form:growl"  listener="#{commodityManager.addCommodityRoyalty}" />
                                    </h:selectOneMenu>

                                </f:facet>
                                <p:column >
                                    <f:facet name="header">
                                        <h:outputText value="序号"/>
                                    </f:facet>
                                    <h:outputText value="#{rowIndex}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="部门职位"/>
                                    </f:facet>
                                    <h:outputText value="#{item.departmentPost.departmentPostInfo}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="部门职位"/>
                                    </f:facet>
                                    <h:outputText value="#{item.departmentPost.departmentPostInfo}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="商品金额提成"/>
                                    </f:facet>
                                    <h:inputText value="#{item.priceRoyalty}" size="3" required="true" requiredMessage="请填写提成金额" validatorMessage="填写金额错误" title="提成需大于，小于1；">
                                        <f:validateDoubleRange minimum="0" maximum="1"></f:validateDoubleRange>
                                    </h:inputText>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="商品数量提成"/>
                                    </f:facet>
                                    <h:inputText value="#{item.quantityRoyalty}" size="3" required="true" requiredMessage="请填写提成金额" validatorMessage="填写金额错误" title="提成需大于0">
                                        <f:validateDoubleRange minimum="0" ></f:validateDoubleRange>
                                    </h:inputText>
                                </p:column>
                                <p:column>
                                    <p:commandLink action="#{commodityManager.deleteCommodityRoyalty(item)}" update="form2:commodityRoyalties,form:growl" >
                                        <p:graphicImage value="/resources/images/delete.png"/>
                                    </p:commandLink>
                                </p:column>
                            </p:dataTable>
                            <f:facet name="footer">
                                <h:panelGroup>
                                    <p:commandButton value="保存" actionListener="#{commodityManager.save}"   update="form1:commodities,form:growl" oncomplete="handleSaveRequest(xhr, status, args)"></p:commandButton>
                                    <p:commandButton value="取消" onclick="commodityDialog.hide()"></p:commandButton>
                                </h:panelGroup>
                            </f:facet>
                        </h:panelGrid>
                    </h:form>
                </p:dialog>

                <p:dialog header="编辑套系商品" widgetVar="packageCommodityDialog"
                          modal="true" draggable="false" width="800" closeListener="#{commodityManager.cancellCreate}"  onCloseUpdate="form:grow1">
                    <h:form id="form3">
                        <h:panelGrid columns="2" id="packageDisplay" columnClasses="columnTop,columnTop">
                            <p:dataTable value="#{commodityManager.selectedCommodity.subCommodityCollection}" var="item" id="subcommodities" rowIndexVar="rowIndex"  >
                                <f:facet name="header" >
                                    <h:selectOneMenu  value="#{commodityManager.selectedCommodity}" >
                                        <f:selectItems value="#{commodityManager.packageCommodityItems}"></f:selectItems>
                                        <p:ajax event="change" update="form3:subcommodities"  />
                                    </h:selectOneMenu>
                                </f:facet>
                                <p:column >
                                    <f:facet name="header">
                                        <h:outputText value="序号"/>
                                    </f:facet>
                                    <h:outputText value="#{rowIndex}"/>
                                </p:column>
                                <p:column >
                                    <f:facet name="header">
                                        <h:outputText value="名称"/>
                                    </f:facet>
                                    <h:outputText value="#{item.subcommodity.name}"/>
                                </p:column>
                                <p:column >
                                    <f:facet name="header">
                                        <h:outputText value="编码"/>
                                    </f:facet>
                                    <h:outputText value="#{item.subcommodity.number}"/>
                                </p:column>
                                <p:column >
                                    <f:facet name="header">
                                        <h:outputText value="数量"/>
                                    </f:facet>
                                    <h:inputText value="#{item.amount}"/>
                                </p:column>
                                <p:column>
                                    <f:facet name="header">
                                        <h:outputText value="属性"/>
                                    </f:facet>
                                    <h:outputText value="#{item.subcommodity.property}"/>
                                </p:column>

                                <p:column headerText="删除">
                                    <p:commandLink action="#{commodityManager.deleteSubcommodity(item)}" update="form3:subcommodities" >
                                        <p:graphicImage value="/resources/images/delete.png"/>
                                    </p:commandLink>
                                </p:column>
                            </p:dataTable>
                            <p:dataTable id="selectedCommodities" value="#{commodityManager.commodityListForSelection}" var="item" rowIndexVar="rowIndex">
                                <f:facet name="header">
                                    <h:panelGrid columns="2">
                                        <h:selectOneMenu value="#{commodityManager.commodityProperty}" id="commodityPropertyMenu">
                                            <f:selectItem itemLabel="商品属性" itemValue="0"></f:selectItem>
                                            <f:selectItems value="#{applicationBean.commdoityPropertyItemsWithoutPackage}"></f:selectItems>
                                            <p:ajax  update="form3:selectedCommodities,form3:selectedCommodityTypes" listener="#{commodityManager.searchByPropertyForSelection}" />
                                        </h:selectOneMenu>
                                        <h:selectOneMenu id="selectedCommodityTypes" value="#{commodityManager.commodityType}">
                                            <f:selectItem itemLabel="商品类型" itemValue="0"></f:selectItem>
                                            <f:selectItems value="#{commodityManager.commodityTypeItems}"></f:selectItems>
                                            <p:ajax  update="form3:selectedCommodities" listener="#{commodityManager.searchByTypeForSelection}" />
                                        </h:selectOneMenu>
                                    </h:panelGrid>
                                </f:facet>
                                <p:column >
                                    <f:facet name="header">
                                        <h:outputText value="序号"/>
                                    </f:facet>
                                    <h:outputText value="#{rowIndex}"/>
                                </p:column>
                                <p:column >
                                    <f:facet name="header">
                                        <h:outputText value="名称"/>
                                    </f:facet>
                                    <h:outputText value="#{item.name}"/>
                                </p:column>
                                <p:column >
                                    <f:facet name="header">
                                        <h:outputText value="编码"/>
                                    </f:facet>
                                    <h:outputText value="#{item.number}"/>
                                </p:column>
                                <p:column  >
                                    <f:facet name="header">
                                        <h:outputText value="单价"/>
                                    </f:facet>
                                    <h:outputText value="#{item.uitPrice}"/>
                                </p:column>
                                <p:column headerText="添加">
                                    <p:commandLink action="#{commodityManager.addSubcommodity(item)}" update="form3:subcommodities,form:growl" >
                                        <p:graphicImage value="/resources/images/add.png"/>
                                    </p:commandLink>
                                </p:column>
                            </p:dataTable>
                            <f:facet name="footer">
                                <h:panelGroup>
                                    <p:commandButton value="保存" actionListener="#{commodityManager.save}"   update="form:growl"></p:commandButton>
                                    <p:commandButton value="关闭" onclick="packageCommodityDialog.hide()"></p:commandButton>
                                </h:panelGroup>
                            </f:facet>
                        </h:panelGrid>
                    </h:form>
                </p:dialog>
            </f:view>
        </ui:define>
    </ui:composition>
</html>
